package com.sjgs.gis.config;

import com.mongodb.Block;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.connection.ClusterSettings;
import com.mongodb.connection.ConnectionPoolSettings;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Configuration
@ConditionalOnProperty(value = "spring.data.mongodb.repositories.enabled", havingValue = "true")
@ConfigurationProperties(prefix = "spring.data.mongodb.repositories")
@Import({MongoReactiveDataAutoConfiguration.class})
@EnableReactiveMongoRepositories
public class MongoRepositoriesConfig {

    private Boolean enabled = false;
    private Integer maxSize = 10;
    private Integer minSize = 5;

    /**
     * Reactive Mongo pool setting
     *
     * @param mongoProperties
     * @return
     */
    @Bean
    @ConditionalOnBean(name = "mongoProperties")
    MongoClientSettings getMongoClientSettings(MongoProperties mongoProperties) {
        //副本集
        final List<ServerAddress> servers = new ArrayList<ServerAddress>();
        servers.add(new ServerAddress(mongoProperties.getHost(), mongoProperties.getPort()));
        //认证信息
        char[] password = mongoProperties.getPassword(); // the password as a character array
        MongoCredential credential = MongoCredential.createCredential(mongoProperties.getUsername(),
                mongoProperties.getAuthenticationDatabase(),
                password);
        return MongoClientSettings.builder()
                .credential(credential)
                .applyToClusterSettings(new Block<ClusterSettings.Builder>() {
                    public void apply(ClusterSettings.Builder builder) {
                        //TODO set ClusterSettings
                        builder.hosts(servers);
                        builder.serverSelectionTimeout(10L, TimeUnit.SECONDS);
                    }
                })
                .applyToConnectionPoolSettings(new Block<ConnectionPoolSettings.Builder>() {
                    @Override
                    public void apply(ConnectionPoolSettings.Builder builder) {
                        //TODO set ConnectionPoolSettings
                        builder.maxSize(maxSize).minSize(minSize).build();
                    }
                })
                .build();
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public Integer getMaxSize() {
        return maxSize;
    }

    public void setMaxSize(Integer maxSize) {
        this.maxSize = maxSize;
    }

    public Integer getMinSize() {
        return minSize;
    }

    public void setMinSize(Integer minSize) {
        this.minSize = minSize;
    }
}